/* setting directory $HOME/.unihan2 */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xproto.h>

#include "xaux_common.h"
#include "xaux_ext_common.h"
#include "unihan_xaux.h"

#include "setting.h"
#include "xaux_ext_status.h"

gchar *get_home_dir(void);

static GList *IM_list_dup(GList *list);
static char *get_im_name_by_id(GList *im_all_list, char *id);
static GList *IM_get_default_im_list_from_all_list(GList *im_all_list);

#define DEFAULT_IM_LIST_PATH "/usr/share/unihan2/DefaultIM/"
#define DEFAULT_IM_LIST_CN "DefaultIMList.zh_CN"
#define DEFAULT_IM_LIST_HK "DefaultIMList.zh_HK"
#define DEFAULT_IM_LIST_TW "DefaultIMList.zh_TW"

GList *
get_curr_list_by_im_all_list(GList *im_all_list)
{
	gchar *home;
	char listFileName[256];
	char bufIMName[256];
	FILE *pListFile;
	GList *im_list = NULL;

	home = get_home_dir();

	if (!home) {
		return NULL;
	}

	memset(listFileName, 0, sizeof(listFileName));

	strcpy(listFileName, (char *)home);
	strcat(listFileName, UNIHAN_SETTING_DIR);
	if (access(listFileName, F_OK) != 0)
		mkdir(listFileName, 0755); 
	strcat(listFileName, IM_LIST_CURR_FILE);

	pListFile = fopen(listFileName, "r");
	if (!pListFile) {
		im_list = IM_get_default_im_list_from_all_list(im_all_list);
		if (!im_list)
			im_list = IM_list_dup(im_all_list);
		configure_im_list_file_by_im_list(im_list);
		return im_list;
	}

	while (fscanf(pListFile, "%s", bufIMName) != EOF) {
		IMInfo *info;

		info = g_new0(IMInfo, 1);
		info->id = strdup(bufIMName);
		info->cname = get_im_name_by_id(im_all_list, info->id);

		if (info->cname) {
			im_list = g_list_append(im_list, info);
		}
	}

	if (!im_list) {
		im_list = IM_list_dup(im_all_list);
		configure_im_list_file_by_im_list(im_list);
	}

	fclose(pListFile);
	return im_list;
}

gchar *
get_home_dir(void)
{
	gchar *p;
	p = g_strdup(g_get_home_dir());
	return p;
}

static char *
get_im_name_by_id(GList *im_all_list, char *id)
{
	char *name = NULL;
	GList *pIM = im_all_list;
	IMInfo *info;

	while(pIM) {
		info = (IMInfo *)pIM->data;
		if (!strcmp(info->id, id)) {
			name = strdup(info->cname);
			break;
		}
		pIM = g_list_next(pIM);
	}
	return name; 
}

void
free_im_list(GList *list)
{
	GList *l = list;

	while(l) {
		IMInfo *info = (IMInfo *)l->data;
		g_free(info->id);
		g_free(info->cname);
		l = g_list_next(l);
	}
	g_list_free(list);
}

void
configure_im_list_file_by_im_list(GList *list)
{
	FILE *pf;
	GList *l = list;
	char *home = get_home_dir();
	char path[256];

	sprintf(path, "%s%s%s", home, UNIHAN_SETTING_DIR, IM_LIST_CURR_FILE);
	pf = fopen(path, "w");
	if (!pf)
		return;
	while (l) {
		IMInfo *info;
		info = (IMInfo *)l->data;
		fprintf(pf, "%s\n", info->id);
		l = g_list_next(l);
	}
	fclose(pf);
}

static GList *
IM_list_dup(GList *list)
{
	GList *l = NULL;
	IMInfo *info = NULL;
	GList *l2 = list;
	IMInfo *info2 = NULL;

	while (l2) {
		info2 = (IMInfo *)l2->data;

		info = g_new0(IMInfo, 1);
		info->id = g_strdup(info2->id);
		info->cname = g_strdup(info2->cname);

		l = g_list_append(l, info);
		l2 = g_list_next(l2);
	}
	return l;
}

static GList *
IM_get_default_im_list_from_all_list(GList *im_all_list)
{
	FILE *pf = NULL;
	GList *im_list = NULL;
	char bufIMName[256];
	char filename[500];
	char *lang;

	memset(filename, 0, 500);
	lang = getenv("LANG");
	if (strstr(lang, "zh_CN")) {
		strcpy(filename, DEFAULT_IM_LIST_PATH);
		strcat(filename, DEFAULT_IM_LIST_CN);
	} else if (strstr(lang, "zh_HK")) {
		strcpy(filename, DEFAULT_IM_LIST_PATH);
		strcat(filename, DEFAULT_IM_LIST_HK);
	} else if (strstr(lang, "zh_TW")) {
		strcpy(filename, DEFAULT_IM_LIST_PATH);
		strcat(filename, DEFAULT_IM_LIST_TW);
	}

	if (!strlen(filename))
		return NULL;
	pf = fopen(filename, "r");
	if (!pf)
		return NULL;

	while (fscanf(pf, "%s", bufIMName) != EOF) {
		IMInfo *info;

		info = g_new0(IMInfo, 1);
		info->id = strdup(bufIMName);
		info->cname = get_im_name_by_id(im_all_list, info->id);

		if (info->cname) {
			im_list = g_list_append(im_list, info);
		}
	}

	return im_list;
}
